package io.mybatis.example.springboot.controller;

import io.mybatis.common.core.DataResponse;
import io.mybatis.common.core.RowsResponse;

import io.mybatis.example.springboot.model.SysDept;
import io.mybatis.example.springboot.service.SysDeptService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * sys_dept - 部门表
 *
 * @author mkj
 */
@RestController
@RequestMapping("sysDepts")
public class SysDeptController {

  @Autowired
  private SysDeptService sysDeptService;

  @PostMapping
  public DataResponse<SysDeptDO> save(@RequestBody SysDeptDO sysDept) {
    return DataResponse.ok(sysDeptService.save( sysDept));
  }

  @GetMapping
  public RowsResponse<SysDeptDO> findList(@RequestBody SysDeptDO sysDept) {
    return RowsResponse.ok(sysDeptService.findList( sysDept));
  }

  @GetMapping(value = "/{id}")
  public DataResponse<SysDeptDO> findById(@PathVariable("id") Long id) {
    return DataResponse.ok(sysDeptService.findById(id));
  }

  @PutMapping(value = "/{id}")
  public DataResponse<SysDeptDO> update(@PathVariable("id") Long id, @RequestBody SysDeptDO sysDept) {
    sysDept.setId(id);
    return DataResponse.ok(sysDeptService.update( sysDept));
  }

  @DeleteMapping(value = "/{id}")
  public DataResponse<Boolean> deleteById(@PathVariable("id") Long id) {
    return DataResponse.ok(sysDeptService.deleteById(id) == 1);
  }

}
